package com.imay.live.activity.discovery;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.live.adapter.DynamicsGridAdapter;
import com.imay.live.model.FeedModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.view.recyclerview.GridVerticalDividerItemDecoration;
import com.imay.live.R;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * Created by liangzhenxiong on 2017/04/11 0011.
 * 贴文界面
 */

public class PasteActivity extends DicLoadActivity {
    int sinceId;
    int DISCOUNT = 30;

    public static void start(Context context) {
        Intent starter = new Intent(context, PasteActivity.class);
        context.startActivity(starter);
    }

    @Override
    public void onRefreshLoad() {
        super.onRefreshLoad();
        ProtoRequestHelper.task(ProtoHttpFactory.getFeedSuggests(0, DISCOUNT)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                try {
                    Result<ArrayList<FeedModel>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<FeedModel>>>() {
                    }.getType());
                    loadViewControl.refreshLoadView(result.data, result.result);
                    if (result.result == CodeSuccess) {
                        if (result.data != null && !result.data.isEmpty()) {
                            sinceId = result.data.get(result.data.size() - 1).getFeedId();
                        }
                    }

                } catch (Exception e) {
                    loadViewControl.loadError(getResources().getString(R.string.str_loaderror));
                }


            }

            @Override
            public void onRequestError(String command, int code, String message) {
                loadViewControl.loadError(message);

            }
        });
    }

    @Override
    public void onLoadMore() {
        ProtoRequestHelper.task(ProtoHttpFactory.getFeedSuggests(sinceId, DISCOUNT)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                try {
                    Result<ArrayList<FeedModel>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<FeedModel>>>() {
                    }.getType());
                    loadViewControl.morePasteLoadView(result.data, result.result);
                    if (result.result == CodeSuccess) {
                        if (result.data != null && !result.data.isEmpty()) {
                            sinceId = result.data.get(result.data.size() - 1).getFeedId();
                        }
                    }

                } catch (Exception e) {
//                    loadError(getResources().getString(R.string.str_loaderror));
                }

            }

            @Override
            public void onRequestError(String command, int code, String message) {


            }
        });
    }

    @Override
    protected void initAdapte() {
        super.initAdapte();
        mpapecount = 1;
        mAdapter = new DynamicsGridAdapter();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitle(getResources().getString(R.string.str_imay_paste_title));
        mrecyclerView.setLayoutManager(new GridLayoutManager(this, 3));


        mrecyclerView.addItemDecoration(new GridVerticalDividerItemDecoration.Builder(this, 1)
                .colorResId(R.color.divider_ff)
                .sizeResId(R.dimen.divider_size_1x)
                .build());
        mrecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .colorResId(R.color.divider_ff)
                .sizeResId(R.dimen.divider_size_1x)
                .build());
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.iv_discovery_item_cover) {
                    Object multiItemEntity = mAdapter.getData().get(position);
                    if (multiItemEntity instanceof FeedModel) {
                        ArrayList<FeedModel> feedModels = new ArrayList<FeedModel>();
                        FeedModel feedModel = (FeedModel) multiItemEntity;
                        int l = mAdapter.getData().size();
                        int pos = 0;
                        for (int i = 0; i < l; i++) {
                            Object multiItemEntity1 = mAdapter.getData().get(i);
                            if (multiItemEntity1 instanceof FeedModel) {
                                FeedModel temp = (FeedModel) multiItemEntity1;
                                feedModels.add(temp);
                                if (feedModel.getFeedId() == temp.getFeedId()) {
                                    pos = i;
                                }
                            }
                        }
                        DynamicActivity.start(PasteActivity.this, feedModels, pos, getResources().getString(R.string.str_imay_paste_title));
                    }
                }

            }
        });
        onRefreshLoad();
    }
}
